<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>image.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_labs_net_image.js.html">image.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Simple image loader, used for preloading.
<a name="line17"></a> * @author nnaze@google.com (Nathan Naze)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.labs.net.image&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line23"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line24"></a>goog.require(&#39;goog.net.EventType&#39;);
<a name="line25"></a>goog.require(&#39;goog.result.SimpleResult&#39;);
<a name="line26"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * Loads a single image.  Useful for preloading images. May be combined with
<a name="line31"></a> * goog.result.combine to preload many images.
<a name="line32"></a> *
<a name="line33"></a> * @param {string} uri URI of the image.
<a name="line34"></a> * @param {(Image|function(): !Image)=} opt_image If present, instead of
<a name="line35"></a> *     creating a new Image instance the function will use the passed Image
<a name="line36"></a> *     instance or the result of calling the Image factory respectively. This
<a name="line37"></a> *     can be used to control exactly how Image instances are created, for
<a name="line38"></a> *     example if they should be created in a particular document element, or
<a name="line39"></a> *     have fields that will trigger CORS image fetches.
<a name="line40"></a> * @return {!goog.result.Result} An asyncronous result that will succeed
<a name="line41"></a> *     if the image successfully loads or error if the image load fails.
<a name="line42"></a> */
<a name="line43"></a>goog.labs.net.image.load = function(uri, opt_image) {
<a name="line44"></a>  var image;
<a name="line45"></a>  if (!goog.isDef(opt_image)) {
<a name="line46"></a>    image = new Image();
<a name="line47"></a>  } else if (goog.isFunction(opt_image)) {
<a name="line48"></a>    image = opt_image();
<a name="line49"></a>  } else {
<a name="line50"></a>    image = opt_image;
<a name="line51"></a>  }
<a name="line52"></a>
<a name="line53"></a>  // IE&#39;s load event on images can be buggy.  Instead, we wait for
<a name="line54"></a>  // readystatechange events and check if readyState is &#39;complete&#39;.
<a name="line55"></a>  // See:
<a name="line56"></a>  // http://msdn.microsoft.com/en-us/library/ie/ms536957(v=vs.85).aspx
<a name="line57"></a>  // http://msdn.microsoft.com/en-us/library/ie/ms534359(v=vs.85).aspx
<a name="line58"></a>  var loadEvent = goog.userAgent.IE ? goog.net.EventType.READY_STATE_CHANGE :
<a name="line59"></a>      goog.events.EventType.LOAD;
<a name="line60"></a>
<a name="line61"></a>  var result = new goog.result.SimpleResult();
<a name="line62"></a>
<a name="line63"></a>  var handler = new goog.events.EventHandler();
<a name="line64"></a>  handler.listen(
<a name="line65"></a>      image,
<a name="line66"></a>      [loadEvent, goog.net.EventType.ABORT, goog.net.EventType.ERROR],
<a name="line67"></a>      function(e) {
<a name="line68"></a>
<a name="line69"></a>        // We only registered listeners for READY_STATE_CHANGE for IE.
<a name="line70"></a>        // If readyState is now COMPLETE, the image has loaded.
<a name="line71"></a>        // See related comment above.
<a name="line72"></a>        if (e.type == goog.net.EventType.READY_STATE_CHANGE &amp;&amp;
<a name="line73"></a>            image.readyState != goog.net.EventType.COMPLETE) {
<a name="line74"></a>          return;
<a name="line75"></a>        }
<a name="line76"></a>
<a name="line77"></a>        // At this point, we know whether the image load was successful
<a name="line78"></a>        // and no longer care about image events.
<a name="line79"></a>        goog.dispose(handler);
<a name="line80"></a>
<a name="line81"></a>        // Whether the image successfully loaded.
<a name="line82"></a>        if (e.type == loadEvent) {
<a name="line83"></a>          result.setValue(image);
<a name="line84"></a>        } else {
<a name="line85"></a>          result.setError();
<a name="line86"></a>        }
<a name="line87"></a>      });
<a name="line88"></a>
<a name="line89"></a>  // Initiate the image request.
<a name="line90"></a>  image.src = uri;
<a name="line91"></a>
<a name="line92"></a>  return result;
<a name="line93"></a>};
</pre>


</body>
</html>
